/**
 * 
 */
package com.nari.baseinfo.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IFileMapping;
import com.nari.baseinfo.service.IFileMappingService;
import com.nari.coreframe.action.struts2.JSONActionSupport;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-11
 */
public class FileMappingAction extends JSONActionSupport {
	private static final long serialVersionUID = 4033659269917619398L;
	private IFileMappingService iFileMappingService;
	//附件下载
	private String fileMappingUID;
	private InputStream targetFile;
	private String outputFileName;
	
	/**
	 * 文件附件下载
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-11
	 */
	public String downloadAttachFile() throws Exception {
		IFileMapping fileMapping = this.iFileMappingService.queryEntityById(fileMappingUID);
		//文件下载时显示名称
		this.outputFileName=new String(fileMapping.getOrFilename().getBytes(),"ISO-8859-1")+"."+fileMapping.getFileType();
		//下载文件输出流
		this.targetFile = new FileInputStream(new File(fileMapping.getDiskID()+"/"+fileMapping.getSavePath()));
		return "downloadFile";
	}
	
	/**
	 * 清空磁盘缓存文件
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-1-17
	 */
	public void clearTemporaryDiskFile() throws Exception {
		//缓存文件目录
		File temporaryFolder = new File(this.getTempFileFolder());
		if(temporaryFolder.isDirectory()) {
			//读取缓存目录中的文件列表
			File[] fileArray = temporaryFolder.listFiles();
			for(File tempFile: fileArray) {
				//清除缓存文件
				tempFile.delete();
			}
		}
	}

	/**
	 * @return the iFileMappingService
	 */
	@JSON(serialize=false)
	public IFileMappingService getiFileMappingService() {
		return iFileMappingService;
	}

	/**
	 * @param iFileMappingService the iFileMappingService to set
	 */
	public void setiFileMappingService(IFileMappingService iFileMappingService) {
		this.iFileMappingService = iFileMappingService;
	}

	/**
	 * @return the fileMappingUID
	 */
	public String getFileMappingUID() {
		return fileMappingUID;
	}

	/**
	 * @param fileMappingUID the fileMappingUID to set
	 */
	public void setFileMappingUID(String fileMappingUID) {
		this.fileMappingUID = fileMappingUID;
	}

	/**
	 * @return the targetFile
	 */
	public InputStream getTargetFile() {
		return targetFile;
	}

	/**
	 * @param targetFile the targetFile to set
	 */
	public void setTargetFile(InputStream targetFile) {
		this.targetFile = targetFile;
	}

	/**
	 * @return the outputFileName
	 */
	public String getOutputFileName() {
		return outputFileName;
	}

	/**
	 * @param outputFileName the outputFileName to set
	 */
	public void setOutputFileName(String outputFileName) {
		this.outputFileName = outputFileName;
	}
	
}
